import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;

public class DataDisplayTable extends JPanel{
	JPanel tablePanel;
	JTable dataTable;
	DefaultTableModel dbModel;
	
	int id;
	Date date;
	String category, trans, trans_method;
	float income, expense;
	public static final int MONTHLY_SUMMARY = 1;
	public static final int DAILY_SUMMARY = 2;
	private int width,length;
	private DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
	
	DataDisplayTable(DefaultTableModel dtb, int screenMode)  {
		dbModel = dtb;
		centerRenderer.setHorizontalAlignment(JLabel.CENTER);
				
		try{		
			dataTable = new JTable(dbModel){public boolean isCellEditable(int row,int coll){return false;}};
			setGUILayout(screenMode);
			JScrollPane scrollPane = new JScrollPane(dataTable);
			dataTable.setFillsViewportHeight(true);
			dataTable.setAutoCreateRowSorter(true);

			scrollPane.setSize(width, length);
			this.setLayout(null);
			this.add(scrollPane);
			
		}catch(Exception e){e.printStackTrace();};
		
		
	}
	
	public void setGUILayout(int screenMode){
		int columnCount = dataTable.getColumnCount();
		for (int i=0; i<columnCount; i++){
			dataTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
		}
		dataTable.removeColumn(dataTable.getColumnModel().getColumn(0));
		
		
		dataTable.getColumnModel().getColumn(0).setPreferredWidth(10);
		dataTable.getColumnModel().getColumn(1).setPreferredWidth(40);
		if(screenMode == MONTHLY_SUMMARY){
			this.width = 800;
			this.length = 285;
			dataTable.getColumnModel().getColumn(2).setPreferredWidth(50);
			dataTable.getColumnModel().getColumn(4).setPreferredWidth(60);
			dataTable.getColumnModel().getColumn(5).setPreferredWidth(35);
			dataTable.getColumnModel().getColumn(6).setPreferredWidth(25);
			dataTable.getColumnModel().getColumn(7).setPreferredWidth(25);
			dataTable.getColumnModel().getColumn(8).setPreferredWidth(50);
		}
		else if(screenMode == DAILY_SUMMARY){
			this.width = 450;
			this.length = 135;
			dataTable.getColumnModel().getColumn(2).setPreferredWidth(70);
			dataTable.getColumnModel().getColumn(4).setPreferredWidth(40);
		}
	}
    
	public Object getValueAt(int row, int col){
		
		return dataTable.getModel().getValueAt(dataTable.convertRowIndexToModel(row), col);
		
	}
	
	
	public int[] getSelectedRows(){
		
		return dataTable.getSelectedRows();
	}
	
}
